package main

import (
	"context"
	"github.com/bytedance/sonic"
	"github.com/cloudwego/hertz/pkg/app/server/render"
	"github.com/cloudwego/kitex/pkg/klog"
	"github.com/cloudwego/kitex/pkg/rpcinfo"
	"github.com/cloudwego/kitex/pkg/transmeta"
	"github.com/cloudwego/kitex/server"
	"github.com/kitex-contrib/obs-opentelemetry/provider"
	"github.com/kitex-contrib/obs-opentelemetry/tracing"
	"github.com/kitex-contrib/registry-nacos/registry"
	"net"
	"primus/hello-server/handler"
	hello "primus/kitex_gen/hello/helloservice"
	"primus/pkg/constants"
	nacosCli "primus/pkg/nacos"
	"primus/pkg/util"
)

var logFile = "/Users/damon/data/hello-server/logs/"

func Init() {
	//klog.SetLogger(kitexlogrus.NewLogger())
	klog.SetLevel(klog.LevelDebug)
	klog.SetOutput(util.NewLogger(logFile))
	//klog.SetLogger(kitexlogrus.NewLogger())
	/*f, err := os.OpenFile("/data/user/logs/output.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		klog.Error(err)
	}
	defer f.Close()
	klog.SetOutput(f)*/

	//tracer2.InitJaeger(constants.HelloServiceName)
	//dal.Init()
}

func main() {
	render.ResetJSONMarshal(sonic.ConfigStd.Marshal)
	conf, err := nacosCli.NewNacosConfig("121.37.173.206", 8848, "")
	if err != nil {
		klog.Error(err)
	}

	p := provider.NewOpenTelemetryProvider(
		provider.WithServiceName(constants.HelloServiceName),
		provider.WithExportEndpoint("121.37.173.206:4317"),
		provider.WithInsecure(),
	)
	defer p.Shutdown(context.Background())

	Init()

	svr := hello.NewServer(
		new(handler.HelloServiceImpl),
		server.WithSuite(tracing.NewServerSuite()),
		server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: constants.HelloServiceName}),
		server.WithServiceAddr(&net.TCPAddr{
			Port: 1000,
		}),
		//server.WithListener(),
		server.WithRegistry(registry.NewNacosRegistry(conf)),

		//server.WithCodec(codec.NewDefaultCodecWithSizeLimit(1024*1024*10)),

		//server.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FrugalRead|thrift.FrugalWrite|thrift.FastRead|thrift.FastWrite)),
		//连接多路复用(mux)，GRPC默认多路复用
		server.WithMuxTransport(),
		server.WithMetaHandler(transmeta.ServerTTHeaderHandler),
	)

	err = svr.Run()

	if err != nil {
		klog.Fatal(err.Error())
	}
}
